﻿using System;
using System.Data;
using MyFloridaHouse.libSvDTO;

namespace MyFloridaHouse.DataAccess.Extensions
{
	/// <summary>
	/// LayerDTODataContract
	/// </summary>
    public static class LayerDTODataContract
    {
		/// <summary>
		/// DataContract.
		/// </summary>
		/// <param name="rdr">The RDR.</param>
		/// <returns>Object: <see cref="LayerDTO"/></returns>
        public static LayerDTO DataContract(IDataReader rdr)
        {
            // this is the correct way to ensure no null's crash the application
            // use the "null-coalescing" operator "??" to test for null and substitute a default value
            //newItem.layerid = rdr["layerid"] is DBNull ? 0 : Convert.ToInt32(rdr["layerid"]);
            // but assume there are no nulls for speed
            var newItem = new LayerDTO
                          	{
                          		layerid = (int) rdr["layerid"],
                                prefix = rdr["layerprefix"].ToString(),
                          		layername = rdr["layername"].ToString(),
                          		layerabbr = rdr["layerabbr"].ToString(),
                          		source = rdr["layersource"].ToString(),
                          		layerYear = rdr["layerYear"].ToString(),
                          		totalshapes = Convert.ToInt32(rdr["totalshapes"]),
                          		zoommax = (int) rdr["zoommax"],
                          		zoommin = (int) rdr["zoommin"],
                          		defaultstrokecolor = rdr["defaultstrokecolor"].ToString(),
                          		defaultfillcolor = rdr["defaultfillcolor"].ToString(),
                          		isConfigLayer = (bool) rdr["isConfigLayer"],
                          		isGraphLayer = (bool) rdr["isGraphLayer"],
                          		isFindLayer = (bool) rdr["isFindLayer"],
                          		isStoreInMemory = (bool) rdr["isStoreInMemory"],
                                isBuildLayer = (bool)rdr["isBuildLayer"]
                          	};
			    //jdm: need to check for null on these two items for layers that aren't building blocks
                if (string.Equals(rdr["districtBuildIndex"].ToString(), ""))
                {
                    newItem.districtBuildIndex = -1;
                }
                else
                {
                    newItem.districtBuildIndex = (int)rdr["districtBuildIndex"];
                }
                if (string.Equals(rdr["displayOrder"].ToString(), ""))
                {
                    newItem.displayOrder = -1;
                }
                else
                {
                    newItem.displayOrder = (int)rdr["displayOrder"];
                }
                if (string.Equals(rdr["displayDictionaryID"].ToString(), ""))
                {
                    newItem.displayDictionaryID = -1;
                }
                else
                {
                    newItem.displayDictionaryID = (int)rdr["displayDictionaryID"];
                }
            return newItem;
        }
    }
}
